<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){

    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
	$data=array('title'=>$_POST['title'],'status'=>1);
	$resultBox=$_POST['resultBox'];
    if(trim($_POST['title'])==""){
        jumpajax('300','角色名称不能为空','');
    }
	if(strpos($resultBox,",")>0){
       $ids=explode(",",$resultBox);
	}
 	if($id==0){
	    $id=$ezhand->insert(MOD,$data);
        if(!empty($ids)){
		foreach($ids as $v){
		    $info=array('role_id'=>$id,'menu_id'=>$v);
			$ezhand->insert('menu_access',$info);
		}
        }
		jumpajax('200','保存成功','','closeCurrent');
	}
	else{
		$ezhand->delete('menu_access','role_id="'.$id.'"');
        if(!empty($ids)){
		foreach($ids as $v){
		    $info=array('role_id'=>$id,'menu_id'=>$v);
			$ezhand->insert('menu_access',$info);
		}
		
        }
	    $ezhand->update(MOD,$data,'id="'.$id.'"');
		jumpajax('200','修改成功','','closeCurrent');
	}
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	
	$arr=$ezhand->select_all('menu','id,title,`parent_id`,node,`mod`,`act`','1="1" order by listorder desc,`mod` desc');
	foreach($arr as $a){
		if($a['parent_id']==0){
			    $menus[$a['id']]['children']=array();
				$menus[$a['id']]['title']=$a['title'];
				$menus[$a['id']]['id']=$a['id'];
				$menus[$a['id']]['node']=$a['node'];
			}
			else{
			    $menus[$a['parent_id']]['children'][]=$a;
			}
		}
	
    if($id==0){
	    $row=array();
		$role_menu_arr=array();
	}
	else{
	    $row=$ezhand->select(MOD,'*','id="'.$id.'"');
		$role_menu_arr=$ezhand->select_2_field('menu_access','menu_id,id','role_id="'.$id.'"');
	}
}
?>